import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    //网站默认登录页面
    path: '/index',
    name: 'index',
    component: resolve => require(['../views/login/Login.vue'], resolve),
    meta: { isPublic: true }
  },
  {
    //注册页面
    path: '/register',
    name: 'register',
    component: resolve => require(['../views/login/Register.vue'], resolve),
    meta: { isPublic: true }
  },
  {
    //忘记密码页面
    path: '/forget',
    name: 'forget',
    component: resolve => require(['../views/login/Forget.vue'], resolve),
    meta: { isPublic: true }
  },
  {
    //登录页面
    path: '/',
    name: 'login',
    component: resolve => require(['../views/login/Login.vue'], resolve),
    meta: { isPublic: true }
  }, {
    //登录页面
    path: '/login',
    name: 'login',
    component: resolve => require(['../views/login/Login.vue'], resolve),
    meta: { isPublic: true }
  },
  {
    //其他页面路由到404页面
    path: "/404",
    name: "404",
    component: resolve => require(['../views/404/404.vue'], resolve),
    meta: { isPublic: true }
  },
  {
    path: '/home',
    component: resolve => require(['../views/home/Home.vue'], resolve),
    children: [
      {
        //登录后页面根页面
        path: '/home',
        name: 'root',
        component: resolve => require(['../views/home/Root.vue'], resolve)
      },
      {
        //用户中心页面
        path: 'center',
        name: 'center',
        component: resolve => require(['../views/home/EditInfo.vue'], resolve)
      },
      {
        //查看用户页面
        path: 'user/:id/show',
        name: 'user',
        component: resolve => require(['../views/home/UserShow.vue'], resolve)
      },
      {
        //创建项目页面
        path: 'createProjects',
        name: 'createProjects',
        component: resolve => require(['../views/project/CreateProject.vue'], resolve)
      },
      {
        //这个id还是需要携带过去的，这是具体哪个项目的id，不是用户的id
        path: 'project/:id/',
        component: resolve => require(['../views/project/Detail.vue'], resolve),
        children: [
          {
            //具体项目默认页面|项目管理页面
            path: 'index',
            name: 'project',
            component: resolve => require(['../views/project/Index.vue'], resolve)
          },
          {
            //项目配置页面
            path: 'config',
            name: 'config',
            component: resolve => require(['../views/project/Config.vue'], resolve)
          },





        ]
      },


    ]
  },
  {
    //其他页面路由到404页面
    path: "*",
    name: "others",
    component: resolve => require(['../views/404/404.vue'], resolve),
    meta: { isPublic: true }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//使用路由守卫，对需要用户权限的页面进行登录操作
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    Vue.prototype.$message({
      type: 'error',
      message: '请先登录'
    })
    return next('/login')
  }
  next()
})


export default router
